package cn.ac.cintcm.prespring.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * ������id:id1,id2,id3...Ϊ�е�λ���������ļ���id����Ӧ����id�������Ӧ
 * �Լ�id1,id2,id3....�ĸ�������Ӧid�Ķ�Ӧ��ϵ
 * ����id����Ϊ-1
 * @author ��ǰ�� Franklin He(franklinheqf@gmail.com)
 *
 */

public class IndexFile {
    HashMap<Integer, List<Integer>> idListIndex;
    HashMap<Integer, List<Integer>> sizeListIndex;
    
    public IndexFile(String fileName) throws IOException {
        idListIndex = FileUtil.structureRead(fileName);
        sizeListIndex = indexSize(idListIndex);
    }
    
    public IndexFile() {
        idListIndex = new HashMap<Integer, List<Integer>>();
        sizeListIndex = new HashMap<Integer, List<Integer>>();
    }
    
    public IndexFile(HashMap<Integer, List<Integer>> idListIndex) throws IOException {
        this.idListIndex = idListIndex;
        sizeListIndex = indexSize(idListIndex);
    }

    public HashMap<Integer, List<Integer>> getIdListIndex() {
        return idListIndex;
    }

    public void setIdListIndex(HashMap<Integer, List<Integer>> idListIndex) {
        this.idListIndex = idListIndex;
    }

    public HashMap<Integer, List<Integer>> getSizeListIndex() {
        return sizeListIndex;
    }

    public void setSizeListIndex(HashMap<Integer, List<Integer>> sizeListIndex) {
        this.sizeListIndex = sizeListIndex;
    }

    /**
     * ����id1,id2,id3....�ĸ�������Ӧid�Ķ�Ӧ��ϵ������
     * @param idListIndex id����Ӧ����id�������Ӧ
     * @return
     */
    public HashMap<Integer, List<Integer>> indexSize(
            HashMap<Integer, List<Integer>> idListIndex) {
        HashMap<Integer, List<Integer>> sizeIndex =  
                        new HashMap<Integer, List<Integer>>();
        if (idListIndex != null) {
            for (Integer i : idListIndex.keySet()) {
                List<Integer> idList = idListIndex.get(i);
                List<Integer> list = sizeIndex.get(idList.size());
                if (list == null) {
                    list = new ArrayList<Integer>();
                    sizeIndex.put(idList.size(), list);
                }
                list.add(i);
            }
        }
        return sizeIndex;
    }

    public void add(String idstr) {
        if (idstr != null) {
            List<Integer> idList = new ArrayList<Integer>();
            int id = FileUtil.parseLine(idstr, idList);
            idListIndex.put(id, idList);
            int size = idList.size();
            List<Integer> list = sizeListIndex.get(size);
            if (list == null) {
                list = new ArrayList<Integer>();
                sizeListIndex.put(size, list);
            }
            list.add(id);
        }
        
    }

}
